﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyLib.MyBase.MyWeb;
using MyChocCuoi.Setting;
using MyLoad.LoadStatic;
using System.Data;
using MyChocCuoi.DB.Content;
using MyLib.MyUtility;
using MyChocCuoi.DB.Member;

namespace MyLoad.LoadContent
{

    public class LHot : MyLoadWeb
    {
        bool IsHot = true;
        public News.NewsType mNewsType = News.NewsType.Nothing;

        string TemplatePathRepeatVideo = "~/Templates/Content/NewsItemVideo.html";
        public LHot(MyASHX CurrentPage, bool IsHot)
        {
            this.IsHot = IsHot;
            this.CurrentPage = CurrentPage;
            TemplatePath = "~/Templates/Content/Hot.html";
            TemplatePathRepeat = "~/Templates/Content/NewsItem.html";
            Init();
        }
        // Hàm trả về chuỗi có chứa mã HTML
        protected override string BuildHTML()
        {
            try
            {
                News mNews = new News();

                DataTable mTable = new DataTable();

                mTable = mNews.Search(0, 0, 20, string.Empty, News.Status.Active, mNewsType, 0, (IsHot ? " LikeCount DESC, ViewCount DESC" : "NewsID DESC"));

                if (mTable.Rows.Count < 1)
                    return string.Empty;

                List<NewsObject> mList = NewsObject.ConvertList(mTable);

                StringBuilder mBuilder = new StringBuilder(string.Empty);
                foreach (NewsObject mObject in mList)
                {
                    string[] arr = { mObject.MemberID.ToString(), MemberObject.GetImagePath(mObject.MemberID),
                                       mObject.MemberName,mObject.DetailLink(), mObject.Title, 
                                   MyConvert.DateTime2Text(mObject.CreateDate), 
                                   mObject.ViewCount.ToString(), mObject.Image};
                    if (mObject.TypeID == (int)News.NewsType.Video)
                    {
                        mBuilder.Append(LoadTemplateByArray(TemplatePathRepeatVideo, arr));
                    }
                    else
                    {
                        mBuilder.Append(LoadTemplateByArray(TemplatePathRepeat, arr));
                    }
                }
                return LoadTemplateByArray(TemplatePath, new string[] { mBuilder.ToString() });

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
